<?php
function generateUniqueId($conn, $length = 7) {
    $characters = '0123456789';
    $id = '';
    do {
        $id = '';
        for ($i = 0; $i < $length; $i++) {
            $id .= $characters[rand(0, strlen($characters) - 1)];
        }
        $result = $conn->query("SELECT 1 FROM exam_user WHERE id = '$id'");
        if ($result !== false && $result->num_rows > 0) {
            $id = ''; 
        } else {
            break;
        }
    } while ($id === '');
    return $id;
}
require '../../cofd/common.php';
$id = generateUniqueId($conn);
$name = $_GET['name'];
$attack = $_GET['attack'];
$url = $_GET['url'];
$sql = "SELECT * FROM `exam_user` WHERE username='$name'";
$result = $conn -> query($sql);
if ($result !== false && $result->num_rows > 0) {
    echo json_encode(array('code' => -1, 'msg' => '用户名已存在'));
    exit();
}

$sql = "INSERT INTO `exam_user`(`username`, `password`, `mail`, `id`) VALUES ('$name','$attack','$url','$id')";
$conn -> query($sql);
echo json_encode(array('code' => 1, 'msg' => '添加成功'));
$conn -> close();